<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    // 并查集
    class UnionFind {
        constructor(n) {
            this.id = new Array(n);
            for (let i = 0; i < n; i++) {
                this.id[i] = i;
            }
        }

        find(p) {
            if (p < 0 || p >= this.id.length) return;

            return this.id[p];
        }

        isConnected(p, q) {
            return this.find(p) === this.find(q)
        }

        unionElements(p, q) {
            let pId = this.find(p),
                qId = this.find(q);
            if (pId === qId) return;

            for (let i = 0; i < this.id.length; i++) {
                if (this.id[i] === pId) {
                    this.id[i] = qId
                }
            }
        }
    }
</script>
</body>
</html>